﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using WebAppIdentity.DbContext;
using WebAppIdentity.Services;

namespace WebAppIdentity.Controllers
{
	/// <summary>
	/// 第三方API管理
	/// </summary>
	[ApiController]
	[Route("[controller]")]
	public class ThirdClientController : ControllerBase
	{
		private readonly IThirdClientService _thirdClientService;
		private readonly IMemoryCache _memoryCache;

		public ThirdClientController(IThirdClientService thirdClientService, IMemoryCache memoryCache)
		{
			_thirdClientService = thirdClientService;
			_memoryCache = memoryCache;
		}

		/// <summary>
		/// 添加第三方调用
		/// </summary>
		/// <param name="appId"></param>
		/// <returns></returns>
		[HttpPost("AddThridClient")]
		public async Task<ThirdClient?> AddThridClient(string appId)
		{
			return await _thirdClientService.CreateThirdClient(appId);
		}

		/// <summary>
		/// 第三方api授权列表
		/// </summary>
		/// <returns></returns>
		[HttpGet("ThridClientList")]
		public async Task<IList<ThirdClient>> ThridClientList()
		{
			return await _thirdClientService.QueryList();
		}

		/// <summary>
		/// 通过appkey与appSecret获取token
		/// </summary>
		/// <param name="thirdClient"></param>
		/// <returns></returns>
		[HttpPost("GetToken")]
		[AllowAnonymous]
		public async Task<string> GetToken(ThirdClient thirdClient)
		{
			return await _thirdClientService.GetToken(thirdClient);
		}
	}
}
